using System;
using System.Reflection;
using Nullables;

namespace Sedna.Util
{
	public static class Utility
	{
	    public static bool IsNullOrEmpty(String str)
	    {
            return (str == null || String.Empty.Equals(str.Trim()));
	    }
	    
		public static bool IsEqualOrNull(Object x, Object y)
		{
			return ((x != null && x.Equals(y)) || (x == null && y == null));
		}

        public static Object GetIdentity(Object entity)
        {
            PropertyInfo identityProperty = (entity == null ? null : entity.GetType().GetProperty("Id"));
            return (identityProperty == null ? null : identityProperty.GetValue(entity, null));
        }
	    
	    public static Object GetPropertyValue(Object obj, String propertyName)
	    {
            PropertyInfo pi = (obj == null ? null : obj.GetType().GetProperty(propertyName));
            return (pi == null ? null : pi.GetValue(obj, null));
	    }
	    
	    public static bool IsPersisted(Object entity)
	    {
            Object identity = GetIdentity(entity);
            return (identity != null && !0.Equals(identity));
	    }

        public static bool IsPersistent(Object entity)
        {
            return (entity == null ? false : entity.GetType().GetProperty("Id") != null);
        }			    	    
	}
}
